#include "selfinfowidget.h"
#include "debug.h"

selfInfoWidget::selfInfoWidget(QWidget *parent) : QDialog(parent)
{
    // 1. 设置整个窗口的属性
    this->setFixedSize(400,150);
    this->setWindowFlag(Qt::FramelessWindowHint);
    // 窗口被关闭时, 自动销毁这个对话框对象.
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(5,0,5,0);
    glayout->setHorizontalSpacing(5);
    glayout->setAlignment(Qt::AlignTop);
    this->setLayout(glayout);

    // 3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75,75);
    avatarBtn->setIconSize(QSize(75,75));
    avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
    avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    glayout->addWidget(avatarBtn,0,0,3,1);

    // 显示的样式做一下统一
    QString labelStyle = "QLabel { font-size: 14px; font-weight: 800; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QString editStyle = "QLineEdit { border: none; border-radius:5px; padding-left:2px; }";
    int height = 30;
    QSize btnSize(70,35);

    // 4. 添加用户的 id 的显示
    idTag = new QLabel();
    idTag->setFixedSize(50,height);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    idTag->setText("序号:");
    idTag->setStyleSheet(labelStyle);

    idLabel = new QLabel();
    idLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    idLabel->setStyleSheet(labelStyle);

    // 5. 添加用户的名字的显示
    nameTag = new QLabel();
    nameTag->setFixedSize(50,height);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    nameTag->setText("昵称:");
    nameTag->setStyleSheet(labelStyle);

    nameLabel = new QLabel();
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameLabel->setStyleSheet(labelStyle);

    nameEdit = new QLineEdit();
    nameEdit->setFixedHeight(height);
    nameEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameEdit->setStyleSheet(editStyle);
    nameEdit->hide();       // 修改的时候再出现

    nameModifyBtn = new QPushButton();
    nameModifyBtn->setFixedSize(btnSize);
    nameModifyBtn->setIconSize(QSize(20,20));
    nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    nameModifyBtn->setStyleSheet(btnStyle);

    nameSubmitBtn = new QPushButton();
    nameSubmitBtn->setFixedSize(btnSize);
    nameSubmitBtn->setIconSize(QSize(20,20));
    nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameSubmitBtn->hide();

    // 6. 添加个性签名
    descTag = new QLabel();
    descTag->setFixedSize(50,height);
    descTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    descTag->setText("签名:");
    descTag->setStyleSheet(labelStyle);

    descLabel = new QLabel();
    descLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descLabel->setStyleSheet(labelStyle);

    descEdit = new QLineEdit();
    descEdit->setFixedHeight(height);
    descEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descEdit->setStyleSheet(editStyle);
    descEdit->hide();       // 修改的时候再出现

    descModifyBtn = new QPushButton();
    descModifyBtn->setFixedSize(btnSize);
    descModifyBtn->setIconSize(QSize(20,20));
    descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    descModifyBtn->setStyleSheet(btnStyle);

    descSubmitBtn = new QPushButton();
    descSubmitBtn->setFixedSize(btnSize);
    descSubmitBtn->setIconSize(QSize(20,20));
    descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    descSubmitBtn->setStyleSheet(btnStyle);
    descSubmitBtn->hide();

    // 7. 添加电话
    phoneTag = new QLabel();
    phoneTag->setFixedSize(50,height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    phoneTag->setText("电话:");
    phoneTag->setStyleSheet(labelStyle);

    phoneLabel = new QLabel();
    phoneLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    phoneLabel->setStyleSheet(labelStyle);

    phoneEdit = new QLineEdit();
    phoneEdit->setFixedHeight(height);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    phoneEdit->setStyleSheet(editStyle);
    phoneEdit->hide();       // 修改的时候再出现

    phoneModifyBtn = new QPushButton();
    phoneModifyBtn->setFixedSize(btnSize);
    phoneModifyBtn->setIconSize(QSize(20,20));
    phoneModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    phoneModifyBtn->setStyleSheet(btnStyle);

    phoneSubmitBtn = new QPushButton();
    phoneSubmitBtn->setFixedSize(btnSize);
    phoneSubmitBtn->setIconSize(QSize(20,20));
    phoneSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->hide();

    // 8. 添加验证码
    verifyCodeTag = new QLabel();
    verifyCodeTag->setFixedSize(50,height);
    verifyCodeTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    verifyCodeTag->setText("验证码:");
    verifyCodeTag->setStyleSheet(labelStyle);
    verifyCodeTag->hide();

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(height);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    verifyCodeEdit->setStyleSheet(editStyle);
    verifyCodeEdit->hide();       // 修改的时候再出现

    getVerifyCodeBtn = new QPushButton();
    getVerifyCodeBtn->setFixedSize(70,height);
    getVerifyCodeBtn->setText("获取验证码");
    getVerifyCodeBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(231, 231, 231); }");
    getVerifyCodeBtn->hide();

    // 9. 添加到布局管理器. 第 0 列被头像占用了. 下列内容都是从第一列开始往后排
    glayout->addWidget(idTag,0,1);
    glayout->addWidget(idLabel,0,2);

    glayout->addWidget(nameTag,1,1);
    glayout->addWidget(nameLabel,1,2);
    glayout->addWidget(nameModifyBtn,1,3);

    glayout->addWidget(descTag,2,1);
    glayout->addWidget(descLabel,2,2);
    glayout->addWidget(descModifyBtn,2,3);

    glayout->addWidget(phoneTag,3,1);
    glayout->addWidget(phoneLabel,3,2);
    glayout->addWidget(phoneModifyBtn,3,3);

#ifdef TEST_UI
    idLabel->setText("1234");
    nameLabel->setText("小短腿");
    descLabel->setText("从今天开始认真敲代码");
    phoneLabel->setText("15019735211");
#endif

    // 10. 连接信号槽
    initSingalSlot();

}

void selfInfoWidget::leaveEvent(QEvent *event)
{
    (void) event;
    this->close();
    QDialog::leaveEvent(event);
}

void selfInfoWidget::initSingalSlot()
{
    // 目前只是简单测试后面还需要通过服务器实现真正修改
    // 修改昵称
    connect(nameModifyBtn,&QPushButton::clicked,this,[=](){
        // 把当前的 nameLabel 和 nameModifyBtn 隐藏起来
        nameLabel->hide();
        nameModifyBtn->hide();
        glayout->removeWidget(nameLabel);
        glayout->removeWidget(nameModifyBtn);
        // 把 nameEdit 和 nameSubmitBtn 显示出来
        nameEdit->show();
        nameSubmitBtn->show();
        glayout->addWidget(nameEdit,1,2);
        glayout->addWidget(nameSubmitBtn,1,3);
        // 把输入框的内容进行设置.
        nameEdit->setText(nameLabel->text());

    });

    // 修改签名
    connect(descModifyBtn,&QPushButton::clicked,this,[=](){
        // 把当前的 descLabel 和 descModifyBtn 隐藏起来
        descLabel->hide();
        descModifyBtn->hide();
        glayout->removeWidget(descLabel);
        glayout->removeWidget(descModifyBtn);
        // 把 nameEdit 和 nameSubmitBtn 显示出来
        descEdit->show();
        descSubmitBtn->show();
        glayout->addWidget(descEdit,2,2);
        glayout->addWidget(descSubmitBtn,2,3);
        // 把输入框的内容进行设置.
        descEdit->setText(nameLabel->text());

    });

    // 修改电话，这里还要验证码
    connect(phoneModifyBtn,&QPushButton::clicked,this,[=](){
        // 把当前的 phoneLabel 和 phoneModifyBtn 隐藏起来
        phoneLabel->hide();
        phoneModifyBtn->hide();
        glayout->removeWidget(phoneLabel);
        glayout->removeWidget(phoneModifyBtn);
        // 把 phoneEdit 和 phoneSubmitBtn 显示出来
        phoneEdit->show();
        phoneSubmitBtn->show();
        glayout->addWidget(phoneEdit,3,2);
        glayout->addWidget(phoneSubmitBtn,3,3);
        // 把输入框的内容进行设置.
        phoneEdit->setText(phoneLabel->text());
        // 还有把验证码显示出来，同时对窗口大小重新设置
        verifyCodeTag->show();
        verifyCodeEdit->show();
        getVerifyCodeBtn->show();
        glayout->addWidget(verifyCodeTag,4,1);
        glayout->addWidget(verifyCodeEdit,4,2);
        glayout->addWidget(getVerifyCodeBtn,4,3);
        this->setFixedSize(400,170);
    });
}

void selfInfoWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 3. 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(rect(), 15, 15);  // 圆角半径
    painter.fillPath(path, QColor("#F3F3F3"));
}
